#include "Entity.h"
#include "../SGD Wrappers/SGD_GraphicsManager.h"
#include "Physics.h"
#include "Game.h"
//Interface
void Entity::Update(float elapsedTime)
{
	SGD::Rectangle camOffset{ { Game::GetInstance()->GetCameraPos().x - 100,
		Game::GetInstance()->GetCameraPos().y - 100 },
		SGD::Size{ (Game::GetInstance()->GetScreenWidth() + 500) / Game::GetInstance()->GetZoomLevel(),
		(Game::GetInstance()->GetScreenHeight() + 500) / Game::GetInstance()->GetZoomLevel() } };
	SGD::Rectangle posRect = { this->GetRect().ComputeCenter(), SGD::Size{ 1, 1 } };
	if (camOffset.IsIntersecting(posRect))
	{
		collisionFlag = 0;
		CalculateRadius();
		PhysicsUpdate(this, elapsedTime);
	}
	else
		collisionFlag = -1;
}

void Entity::Render()
{
	SGD::Rectangle camOffset{ { Game::GetInstance()->GetCameraPos().x - 100,
		Game::GetInstance()->GetCameraPos().y - 100 },
		SGD::Size{ (Game::GetInstance()->GetScreenWidth() + 500) / Game::GetInstance()->GetZoomLevel(),
		(Game::GetInstance()->GetScreenHeight() + 500) / Game::GetInstance()->GetZoomLevel() } };
	SGD::Rectangle posRect = { this->GetRect().ComputeCenter(), SGD::Size{ 1, 1 } };
	if (camOffset.IsIntersecting(posRect))
	{
		SGD::GraphicsManager::GetInstance()->DrawTexture(
			image, position, rotation, size / 2);
	}
}

const SGD::Rectangle Entity::GetRect() const
{
	return SGD::Rectangle{ position, size };
}

void Entity::HandleCollision(const IEntity* other)
{
	(void)other;
}

void Entity::AddRef()
{
	++refCount;
}

void Entity::Release()
{
	--refCount;
	if (0 == refCount)
		delete this;
}

void Entity::CalculateRadius()
{
	radius = size.width / 2;
}